package org.danger.t7.common.redis;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufInputStream;
import io.netty.buffer.ByteBufOutputStream;
import org.danger.t7.common.jackson.ObjectMapperFactory;
//import org.redisson.client.codec.BaseCodec;
//import org.redisson.client.handler.State;
//import org.redisson.client.protocol.Decoder;
//import org.redisson.client.protocol.Encoder;
//import org.redisson.config.Config;
//import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
//
///**
// * Redisson的json序列化器
// */
//public class JsonRedissonAutoConfigurationCustomizer implements RedissonAutoConfigurationCustomizer {
//
//    public static final class JsonRedissonCodec extends BaseCodec {
//
//        public static final JsonRedissonCodec INSTANCE = new JsonRedissonCodec();
//
//        protected final ObjectMapper mapObjectMapper;
//
//        private final Encoder encoder = new Encoder() {
//            @Override
//            public ByteBuf encode(Object in) throws IOException {
//                ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
//                try {
//                    ByteBufOutputStream os = new ByteBufOutputStream(out);
//                    mapObjectMapper.writeValue((OutputStream) os, in);
//                    return os.buffer();
//                } catch (IOException e) {
//                    out.release();
//                    throw e;
//                } catch (Exception e) {
//                    out.release();
//                    throw new IOException(e);
//                }
//            }
//        };
//
//        private final Decoder<Object> decoder = new Decoder<Object>() {
//            @Override
//            public Object decode(ByteBuf buf, State state) throws IOException {
//                return mapObjectMapper.readValue((InputStream) new ByteBufInputStream(buf), Object.class);
//            }
//        };
//
//        public JsonRedissonCodec() {
//            mapObjectMapper = ObjectMapperFactory.objectMapperWithTypeIndicator();
//        }
//
//        @Override
//        public Decoder<Object> getValueDecoder() {
//            return decoder;
//        }
//
//        @Override
//        public Encoder getValueEncoder() {
//            return encoder;
//        }
//    }
//
//    @Override
//    public void customize(Config configuration) {
//        configuration.setCodec(JsonRedissonCodec.INSTANCE);
//    }
//}
